package main

import (
	"ifms/tcp_servicer"
	"log"
	"net/http"
	_ "net/http/pprof"
	"os"
)

var logger *log.Logger = log.New(os.Stdout, "Tcp_Main ", log.LstdFlags|log.Lshortfile)

// will be initilized by build parameter
// -ldflags="-X main.version=$(git describe --always --long --dirty)"

// var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")

var version string

func main() {

	// flag.Parse()
	// if *cpuprofile != "" {
	// 	f, err := os.Create(*cpuprofile)
	// 	if err != nil {
	// 		log.Fatal(err)
	// 	}
	// 	pprof.StartCPUProfile(f)
	// 	logger.Printf("CPU profile at : %v ..... \n", cpuprofile)
	// 	defer pprof.StopCPUProfile()
	// }
	defer profile.Start(profile.CPUProfile).Stop()

	go func() {
		pprofport := os.Getenv("PPROFPORT")
		if len(pprofport) < 2 {
			pprofport = ":6061"
		}
		logger.Println("pprof TCP server staring.... ", http.ListenAndServe("localhost"+pprofport, http.DefaultServeMux), "  started")
	}()

	port := os.Getenv("TPRT") //":9721"

	server := tcp_servicer.CreateTcpServer()

	logger.Printf("TCP Servicer started, running on %s. \n", port)
	logger.Printf("Version: %v \n", version)

	server.Start(port)

}

func checkError(err error) {
	if err != nil {
		logger.Printf("Fatal error: %s\n", err.Error())
		os.Exit(1)
	}
}
